var JS_CHART = function(){
	var owner = this;
	owner.dom_tag = "chart";
	owner.searchCompany = "";
	owner.searchAd = "";
	owner.searchDay = "";
	owner.chartDay = null;
	owner.chartWeek = null;
	owner.chartMonth = null;
	owner.resizeChartTimer = null;
	
	owner.init = function(){
		$("#ipt_"+owner.dom_tag+"_search_company").combobox().combobox("loadData", GLOBAL.getCompanyMatchList(true)).combobox("select", "");
		if(window.localStorage.getItem("adminRoleName")!="超级管理员") {
			$("#ipt_main_search_company").next().hide();
		}
		$("#ipt_"+owner.dom_tag+"_search_ad").combobox().combobox("loadData", GLOBAL.getAdMatchList(true)).combobox("select", "");
		$("#ipt_"+owner.dom_tag+"_search_day").Zebra_DatePicker({
			enableHour: false,
			direction: -1
		});
		owner.chartDay = echarts.init(document.getElementById(owner.dom_tag+"_div_chart_day"), 'macarons');
		owner.chartWeek = echarts.init(document.getElementById(owner.dom_tag+"_div_chart_week"), 'macarons');
		owner.chartMonth = echarts.init(document.getElementById(owner.dom_tag+"_div_chart_month"), 'macarons');
	};
	
	owner.loadChartData = function() {
		owner.searchCompany = $("#ipt_"+owner.dom_tag+"_search_company").textbox("getValue");
		owner.searchAd = $("#ipt_"+owner.dom_tag+"_search_ad").textbox("getValue");
		owner.searchDay = $("#ipt_"+owner.dom_tag+"_search_day").val();
		if($.trim(owner.searchDay)=="") {
			$.messager.alert("提示信息", "请选择日期！");
			return;
		}
		owner.drawChart();
	}
	
	owner.drawChart = function(){
		owner.chartWeek = echarts.init(document.getElementById(owner.dom_tag+"_div_chart_week"));
		owner.chartMonth = echarts.init(document.getElementById(owner.dom_tag+"_div_chart_month"));
		owner.chartDay.showLoading({
			effect: 'dynamicLine'
		});
		owner.chartWeek.showLoading({
			effect: 'dynamicLine'
		});
		owner.chartMonth.showLoading({
			effect: 'dynamicLine'
		});
		owner.formatChartDay();
		owner.formatChartWeek();
		owner.formatChartMonth();
	};
	
	owner.formatChartDay = function(){
		$.ajax({
			type: "get",
			url: "/manager/stat/chart/day",
			cache: false,
			dataType: "json",
			data: {
				company: owner.searchCompany,
				ad: owner.searchAd,
				day: owner.searchDay
			}
		}).done(function(result){
			if(result.isTrue) {
				var clickData = result.tag["today_click_times"];
				var viewData = result.tag["today_view_times"];
				var durationData = result.tag["today_view_duration"];
				var xData = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
				var subtext = "";
				var totalClick=0, totalView=0, totalDuration=0;
				for(var i=0; i<xData.length; i++) {
					if (i>=clickData.length) {
						clickData[i] = clickData[clickData.length-1];
					} else {
						totalClick += clickData[i];
					}
				}
				for(var i=0; i<xData.length; i++) {
					if (i>=viewData.length) {
						viewData[i] = viewData[viewData.length-1];
					} else {
						totalView += viewData[i];
					}
				}
				for(var i=0; i<xData.length; i++) {
					if (i>=durationData.length) {
						durationData[i] = durationData[durationData.length-1];
					} else {
						durationData[i] = parseInt(durationData[i]/60, 10);
						totalDuration += durationData[i];
					}
				}
				subtext +="总点击次数:"+totalClick+", 总访问次数:"+totalView+", 总访问时长:"+totalDuration+"分钟";
				owner.chartDay.setOption(owner.formatChartOption("今日统计", subtext, xData, clickData, viewData, durationData));
				owner.chartDay.hideLoading();
			} else {
				$.messager.alert('提示信息', result.message);
			}
		}).fail(function(){
			$.messager.alert('提示信息', "网络繁忙，请稍后再试！");
		});
	};
	
	owner.formatChartWeek = function(){
		$.ajax({
			type: "get",
			url: "/manager/stat/chart/week",
			cache: false,
			dataType: "json",
			data: {
				company: owner.searchCompany,
				ad: owner.searchAd,
				day: owner.searchDay
			}
		}).done(function(result){
			if(result.isTrue) {
				var clickData = result.tag["week_click_times"];
				var viewData = result.tag["week_view_times"];
				var durationData = result.tag["week_view_duration"];
				var xData = ['星期一','星期二','星期三','星期四','星期五','星期六','星期日'];
				var subtext = "";
				var totalClick=0, totalView=0, totalDuration=0;
				for(var i=0; i<xData.length; i++) {
					if (i>=clickData.length) {
						clickData[i] = clickData[clickData.length-1];
					} else {
						totalClick += clickData[i];
					}
				}
				for(var i=0; i<xData.length; i++) {
					if (i>=viewData.length) {
						viewData[i] = viewData[viewData.length-1];
					} else {
						totalView += viewData[i];
					}
				}
				for(var i=0; i<xData.length; i++) {
					if (i>=durationData.length) {
						durationData[i] = durationData[durationData.length-1];
					} else {
						durationData[i] = parseInt(durationData[i]/60, 10);
						totalDuration += durationData[i];
					}
				}
				for(var i=0; i<xData.length; i++) {
					xData[i] = xData[i]+"["+result.tag["week_date"][i]+"]";
				}
				subtext +="总点击次数:"+totalClick+", 总访问次数:"+totalView+", 总访问时长:"+totalDuration+"分钟";
				owner.chartWeek.setOption(owner.formatChartOption("本周统计", subtext, xData, clickData, viewData, durationData));
				owner.chartWeek.hideLoading();
			} else {
				$.messager.alert('提示信息', result.message);
			}
		}).fail(function(){
			$.messager.alert('提示信息', "网络繁忙，请稍后再试！");
		});
	};
	
	owner.formatChartMonth = function(){
		$.ajax({
			type: "get",
			url: "/manager/stat/chart/month",
			cache: false,
			dataType: "json",
			data: {
				company: owner.searchCompany,
				ad: owner.searchAd,
				day: owner.searchDay
			}
		}).done(function(result){
			if(result.isTrue) {
				var clickData = result.tag["month_click_times"];
				var viewData = result.tag["month_view_times"];
				var durationData = result.tag["month_view_duration"];
				var xData = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
				var subtext = "";
				var totalClick=0, totalView=0, totalDuration=0;
				for(var i=0; i<xData.length; i++) {
					if (i>=clickData.length) {
						clickData[i] = clickData[clickData.length-1];
					} else {
						totalClick += clickData[i];
					}
				}
				for(var i=0; i<xData.length; i++) {
					if (i>=viewData.length) {
						viewData[i] = viewData[viewData.length-1];
					} else {
						totalView += viewData[i];
					}
				}
				for(var i=0; i<xData.length; i++) {
					if (i>=durationData.length) {
						durationData[i] = durationData[durationData.length-1];
					} else {
						durationData[i] = parseInt(durationData[i]/60, 10);
						totalDuration += durationData[i];
					}
				}
				subtext +="总点击次数:"+totalClick+", 总访问次数:"+totalView+", 总访问时长:"+totalDuration+"分钟";
				owner.chartMonth.setOption(owner.formatChartOption("本月统计", subtext, xData, clickData, viewData, durationData));
				owner.chartMonth.hideLoading();
			} else {
				$.messager.alert('提示信息', result.message);
			}
		}).fail(function(){
			$.messager.alert('提示信息', "网络繁忙，请稍后再试！");
		});
	};
	
	owner.formatChartOption = function(title, subtext, xData, clickData, viewData, durationData){
		return {
		    title: {
		        text: title,
		        subtext: subtext,
		        subtextStyle: {
		        	color: '#666666'
		        }
		    },
		    legend: {
		        data: ['点击次数','访问次数','访问时长']
		    },
		    toolbox: {
		    	right: 50,
		        feature: {
		            dataView: {show: true, readOnly: true},
		            magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
		            saveAsImage: {show: true}
		        }
		    },
		    tooltip: {
		    	trigger: 'axis',
		    	formatter: function (params) {
		    		var res = "";
		   			for (var i = 0, l = params.length; i < l; i++) {
		     			res += params[i].seriesName + ' : ' + params[i].data+'<br/>';
		  			}
		   			return res;
		        }
		    },
		    xAxis: [{
		    	boundaryGap: false,
		   		data: xData
		    }],
		    yAxis: [{
	            axisLabel: {
	                type: 'value'
	            }
		    }, {
	            axisLabel: {
	            	name: '点击次数',
	            	nameLocation: 'start',
	                type: 'value'
	            }
		    }],
		    series: [{
	            name: '点击次数',
	            type: 'line',
	            yAxisIndex: 1,
	            clickable: false,
	            data: clickData,
	            markPoint: {
	                data: [
	                    {type: 'max', name: '最大值'},
	                    {type: 'min', name: '最小值'}
	                ]
	            },
	            markLine: {
	                data: [
	                    {type: 'average', name: '平均值'}
	                ]
	            }
	        }, {
	            name: '访问次数',
	            type: 'line',
	            clickable: false,
	            data: viewData,
	            markPoint: {
	                data: [
	                    {type: 'max', name: '最大值'},
	                    {type: 'min', name: '最小值'}
	                ]
	            },
	            markLine: {
	                data: [
	                    {type: 'average', name: '平均值'}
	                ]
	            }
	        }, {
	            name: '访问时长',
	            type: 'line',
	            clickable: false,
	            data: durationData,
	            markPoint: {
	                data: [
	                    {type: 'max', name: '最大值'},
	                    {type: 'min', name: '最小值'}
	                ]
	            },
	            markLine: {
	                data: [
	                    {type: 'average', name: '平均值'}
	                ]
	            }
	        }]
		};
	};
	
	
	
	
	
	
	
	
	
	
	
	
	
	
};